﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;
using Reborn_Server.App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_Lightening : TargetAreaSpell
    {

        private int DamageLow = 165;
        private int DamageHigh = 185;

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            MapItem targetsMapItem = null;
            List<Character> CharactersInUpdateRange = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                    targetsMapItem = mapitem;

                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharactersInUpdateRange.Add((Character)creaturebase);
                }
            }

            targetsMapItem.SpellLayer1.Add(this);
            MapItems.Add(targetsMapItem);

            //Build a list of creatures first
            List<CreatureBase> creaturesToDamage = new List<CreatureBase>();
            foreach (CreatureBase creaturebase in targetsMapItem.CreaturesBases)
            {
                if (creaturebase.HP > 0 && creaturebase.Faction == TargetCreatureBase.Faction)
                    creaturesToDamage.Add(creaturebase);
            }

            //Damage them later, damaging them can cause creatures to be removed from the tile which will break the above iteration
            foreach (CreatureBase creaturebase in creaturesToDamage)
            {
                if (creaturebase.HP > 0 && creaturebase.Faction == TargetCreatureBase.Faction)
                {
                    //If we're attacking a player, and we dont have a flag on them, give them a flag on us.
                    if (creaturebase is Character &&
                        myCreatureBase is Character &&
                        creaturebase != myCreatureBase &&
                        myCreatureBase.hateManager.GetCreatureHateInfo(creaturebase) == null &&
                        spellIcon.Offensive == true)
                    {
                        Character charToAttack = (Character)creaturebase;
                        if (charToAttack.friendsManager.hasNoSDFlag(CastingCharacterID) == false)
                        {
                            if (creaturebase.hateManager.GetCreatureHateInfo(myCreatureBase) == null)
                            {
                                creaturebase.SendMessage("You have been ATTACKED by " + myCreatureBase.Name + "!", "damaged");
                                creaturebase.PlaySound("Ding", creaturebase.MapX, creaturebase.MapY);

                                creaturebase.hateManager.UpdateList(myCreatureBase, 0);


                                charToAttack.AddMapItem(myCreatureBase.currentMapItem);
                            }
                            else
                                creaturebase.hateManager.UpdateList(myCreatureBase, 0);
                        }
                    }

                    int Damage = Managers.GameHandler.random.Next(DamageLow, DamageHigh + 1);
                    Damage = SkillManager.AdjustForCombat(CastLevel, Damage, myCreatureBase.IntelligenceTotal);

                    int ActualDamage = creaturebase.CalculateGetSpellDamagePerResists(Damage, spellIcon.Skill);
                    int absorbedDamage;
                    creaturebase.InflictSpellDamage(ActualDamage, CastingCharacterID, CastingCreatureGUID, spellIcon.Skill, out absorbedDamage);

                    creaturebase.Update_Character();

                    ActualDamage = ActualDamage - absorbedDamage;

                    if (myCreatureBase is Character)
                    {
                        Character character = (Character)myCreatureBase;

                        Message message = new Message();
                        message.CreatureID = character.CreatureID;
                        message.Type = "spelloffense";
                        message.Text = "You shock " + creaturebase.Name + "! (" + ActualDamage.ToString() + ")";

                        if (absorbedDamage != 0)
                            message.Text = message.Text + " ABSORBED " + absorbedDamage.ToString();

                        character.AddMessage(message);
                    }
                    if (creaturebase is Character && creaturebase != myCreatureBase)
                    {
                        Character character = (Character)creaturebase;

                        Message message = new Message();
                        message.CreatureID = character.CreatureID;
                        message.Type = "spelloffense";
                        message.Text = myCreatureBase.Name + " shocks you! (" + ActualDamage.ToString() + ")";

                        if (absorbedDamage != 0)
                            message.Text = message.Text + " ABSORBED " + absorbedDamage.ToString();

                        character.AddMessage(message);
                    }

                    if (creaturebase.HP > 1)
                    {
                        int result = Managers.GameHandler.random.Next(1, 101);
                        if (result > 50 && creaturebase.buffManager.IsStunned() == false)
                        {
                            bool isImmune = false;
                            if (creaturebase is Creature)
                            {
                                Creature targetCrit = (Creature)creaturebase;
                                if (targetCrit.isImmune("Stun") == true)
                                {
                                    myCreatureBase.SendMessage(creaturebase.Name + " is immune to " + spellIcon.Name + "'s stun effect!", "spelldefense");
                                    isImmune = true;
                                }
                            }
                            if (isImmune == false)
                            {
                                Spell_Stun buff = new Spell_Stun();
                                buff.saveBuff = false;
                                if (buff.tryResist(this.spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, creaturebase) == true)
                                {
                                    if (creaturebase != myCreatureBase)
                                        myCreatureBase.SendMessage(creaturebase.Name + " resists " + spellIcon.Name + "!", "spelldefense");

                                    creaturebase.SendMessage("You resist " + myCreatureBase.Name + "'s " + spellIcon.Name + "!", "spelloffense");
                                }
                                else
                                {
                                    Buff oldLight = creaturebase.buffManager.GetBuff("Lightning");
                                    if (oldLight != null)
                                        oldLight.RemoveSpell(false, false);

                                    CreatureBase cb = creaturebase;
                                    buff.Resume(cb, this.spellIcon, 5, CastLevel, CastingCharacterID, CastingCreatureInt, CastingCreatureWis, -1, null);
                                    cb.buffManager.UpdateBuffs();
                                }
                            }
                        }
                    }
                }
            }

            for (int x = 0; x < targetsMapItem.CreaturesBases.Count; x++)
            {
                CreatureBase creaturebase = (CreatureBase)targetsMapItem.CreaturesBases[x];
                if (creaturebase.HP < 1)
                {
                    if (!(creaturebase is Character))
                    {
                        targetsMapItem.CreaturesBases.RemoveAt(x);
                        x--;
                    }
                }
            }
            foreach (Character character in CharactersInUpdateRange)
            {
                character.PlaySound("Lightening", targetsMapItem.MapX, targetsMapItem.MapY);
                character.AddMapItem(targetsMapItem);
            }
        }

    }
}
